<?php
namespace Admin\Controller;
use Think\Controller;
class ModelNav1Controller extends Controller {

	private $m='';
	public function _initialize(){
		if($this->m == ''){
			$this->m = M("Admin_nav1");
		}
	}



	//添加一级导航
	public function Nav1_Add(){
		$Nav1Name = I('post.Nav1Name');
		$Nav1Order = I('post.Nav1Order');
		if(!$Nav1Name || !$Nav1Order){
			exit('e1');		//数据不得为空！
		}
		$data['nav1_name'] = $Nav1Name;
		$data['nav1_order'] = $Nav1Order;
		$res = $this->m->where(array('nav1_name'=>$Nav1Name))->find();
		if($res){
			exit('e2');		//导航名以存在
		}
		if($this->m->create($data)){
			$st = $this->m->add();
			if($st){
				echo $st;
				session('data_Nav1',null);
			}else{
				echo 'e3';
			}
		}else{
			echo 'e4';
		}
	}



	//获取全部一级导航信息
	public function Nav1_Get_List(){
		return Get_Fpage($this->m,$fields='',$where='',$order = 'nav1_order',$pcount = 100);
	}


	//删除一级导航
	public function Nav1_Del($nid=''){
		$nid = I('post.nid');
		echo $this->m->delete($nid);
		session('data_Nav1',null);	//清空一级导航缓存，以便刷新

		$m = M('Admin_nav2');
		$m_Rule = M('Think_auth_rule');
		$arr = explode(',',$nid);
		foreach($arr as $v){
			//先获取二级导航名，并根据导航名删除权限规则表中的规则
			$res_Nav2 = $m->field('nav2_addr')->where(array('nav2_parent'=>$v))->select();
			foreach($res_Nav2 as $k2=>$v2){
				$v2['nav2_addr'];
				$m_Rule->where(array('name'=>$v2['nav2_addr']))->delete();
			}
			//删除二级导航
			$where['nav2_parent'] = $v;
			$st = $m->where($where)->delete();
		}
	}



}